home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 359 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: lrz-muenchen.de!sun2!ua302aa
  2. From: ua302aa@sun2.lrz-muenchen.de (Kurt Watzka)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: What is '?' in C mean....?????
  5. Date: 4 Jan 1996 19:28:33 GMT
  6. Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
  7. Distribution: world
  8. Message-ID: <4ch9p1$kfq@sparcserver.lrz-muenchen.de>
  9. References: <4cgsa8$bm2@wumpus.cc.uow.edu.au> <fcusack-0401961115540001@mudskipper.cac.psu.edu>
  10. NNTP-Posting-Host: sun2.lrz-muenchen.de
  11.  
  12. fcusack@tdx.org (frank.) writes:
  13.  
  14. >> Could anyone here explain to me what is "?" means and what the purpose
  15. >>of using
  16.  
  17. >You can probably find this info in the FAQ!!!!
  18.  
  19. >? : is C's ternary operator. if the condition is met, perform the
  20. >operation after the ?; if the condition is not met, perform the operation
  21. >after the :
  22.  
  23. Replace "perform operation" with "evaluate expression", and I'll
  24. agree with you. If evaluating the expression has any side effects,
  25. and if the result of the conditional expression is not used, the
  26. net effect will be pretty much the same, but this does not explain
  27. the differenct between
  28.  
  29.    if (foo()) bar(); else baz();
  30.  
  31. and
  32.  
  33.    boo = foo() ? bar() : baz();
  34.  
  35. The conditional expression is an _expression_, not just an obfuscated
  36. way to write a certain control flow statement.
  37.  
  38. Consider the following function called Abs() for more than one reason:
  39.  
  40.    int Abs(int x)
  41.    {
  42.       return x < 0 ? -x : x;
  43.    }
  44.  
  45. Kurt
  46. --
  47. | Kurt Watzka                             Phone : +49-89-2180-6254
  48. | watzka@stat.uni-muenchen.de
  49. | ua302aa@sunmail.lrz-muenchen.de
  50.